package com.zzzy.framework.common.util;

import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PDFCtrl;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.word.WordDocumentWriter;
import com.zzzy.common.util.ServletUtils;

import java.util.Objects;

public class PageOfficeUtil {

    public static String getPdfHtml(String openFileUrl, String fileName){
        PDFCtrl pdfCtrl = new PDFCtrl(ServletUtils.getRequest());
        pdfCtrl.setCaption(fileName);
        pdfCtrl.setFileTitle(fileName);
        pdfCtrl.webOpen(openFileUrl);
        return pdfCtrl.getHtml();
    }

    public static String getWordHtml(String openFileUrl, String fileName, OpenModeType openModeType, WordDocumentWriter doc) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(ServletUtils.getRequest());
        if (Objects.nonNull(doc)){
            poCtrl.setWriter(doc);
        }
        if(OpenModeType.docReadOnly == openModeType){
            poCtrl.setCaption(fileName + "（只读）");
        } else {
            poCtrl.setCaption(fileName);
        }
        poCtrl.setFileTitle(fileName);
        poCtrl.webOpen(openFileUrl, openModeType, SecurityUtils.getLoginUser().getNickName());
        return poCtrl.getHtml();
    }

    public static String getWordHtml(String openFileUrl, String fileName, OpenModeType openModeType) {
        return getWordHtml(openFileUrl, fileName, openModeType, null);
    }

    public static String getExcelHtml(String openFileUrl, String fileName, OpenModeType openModeType) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(ServletUtils.getRequest());
        poCtrl.setCaption(fileName);
        poCtrl.setFileTitle(fileName);
        poCtrl.webOpen(openFileUrl, openModeType, SecurityUtils.getLoginUser().getNickName());
        return poCtrl.getHtml();
    }

    public static String getCompareWordHtml(String firstFileUrl, String secondFileUrl, String caption) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(ServletUtils.getRequest());
        poCtrl.setCaption(caption);
        poCtrl.wordCompare(firstFileUrl, secondFileUrl, OpenModeType.docReadOnly, SecurityUtils.getLoginUser().getNickName());
        return poCtrl.getHtml();
    }
}
